package Utils;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import entity.CommonResult;
import entity.user;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {
    public static final String srectKey="wshiljd123";
    public static String gettoken(user user){
        JWTCreator.Builder builder = JWT.create();
        builder.withClaim("userid",user.getSerial())
                .withClaim("username",user.getUsername())
                .withClaim("password",user.getPassword())
                .withClaim("nickname",user.getNickname());
//                .withClaim("sex",user.getSex())
//                .withClaim("headsculptureUrl",user.getHeadsculptureUrl());
//        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.SECOND,10);
//        builder.withExpiresAt(instance.getTime());
        String token = builder.sign(Algorithm.HMAC256(srectKey)).toString();
        return token;
    }
    public static DecodedJWT decodejwt(String token){
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(srectKey)).build();
        DecodedJWT verify = verifier.verify(token);
        return verify;
    }

    public static void main(String[] args) {
        user user = new user();
        user.setSerial("27342324");
        user.setNickname("张三");
        user.setPassword("123");
        user.setUsername("zs");
        System.out.println(JSONUtil.toJSONString(user));
        String gettoken = gettoken(user);
        System.out.println(decodejwt(gettoken));
        CommonResult success = ResultUtil.success(gettoken);
        String s = JSON.toJSONString(success);
        System.out.println(s);
    }
}
